package com.wxl.www.wxl.shiro.utils.AOP.impl.userinfo;

import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.utils.AOP.LoginUserInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class LoginUser implements HandlerMethodArgumentResolver {
    /**
     * 判断是否支持要转换的参数类型
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().isAssignableFrom(UserInfo.class)&&methodParameter.hasParameterAnnotation(LoginUserInfo.class);
    }
    /**
     * 当支持后进行相应的转换 做业务操作
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return (UserInfo)SecurityUtils.getSubject().getPrincipal();
    }

}
